package com.erpanderp.wam.usm.view.leave;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpSession;

import com.erpanderp.wam.usm.model.entity.leave.LeaveType;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.leave.LeaveTypeDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;
@ManagedBean
@ViewScoped
public class LeaveTypesBean implements Serializable {

	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private Map<String, Long> leaveTypesMap = new HashMap<String, Long>();
	
    LeaveTypeDAO leaveTypeDAO = new LeaveTypeDAO();
	
    private List<LeaveType> leaveTypes = new ArrayList<LeaveType>();
	
	private LeaveType leaveType = new LeaveType();

	private Long selectedLeaveTypeId;
	
	private LeaveType selectedLeaveType;
	private WAMUser wamUser = new WAMUser();
	private boolean detailsForLeaveType;

	public LeaveTypesBean() {
		wamUser = ((WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER));
		if(wamUser != null){
		leaveTypes = leaveTypeDAO.getLeaveTypes();
		for (LeaveType leaveType : leaveTypes) {
			leaveTypesMap.put(leaveType.getLeaveType(), leaveType.getLeaveTypeid());
		}
		}else{
			ValidationUtil.gotoLoginPage();
		}

	}

	public void leaveTypeChangeListener(AjaxBehaviorEvent e) {
		setDetailsForLeaveType(true);
		
		if(selectedLeaveTypeId == null || selectedLeaveTypeId == 0)
		{
			setDetailsForLeaveType(false);
		}
		else
		{
			System.out.println("selectedLeaveType........"+selectedLeaveType);
			selectedLeaveType = leaveTypeDAO.getLeaveTypeById(selectedLeaveTypeId);
			if(selectedLeaveType.getDescription() == null || selectedLeaveType.getDescription().equals(""))
			{
				System.out.println("22222222222");
				selectedLeaveType.setDescription("Enter Description");
			}
			
		}

		
		}
	
	public String updateResourceAction() {
		
		System.out.println("updateResourceAction.............");
		leaveTypeDAO.updateLeaveType(selectedLeaveType);
		
		leaveTypesMap.clear();
		leaveTypes = leaveTypeDAO.getLeaveTypes();
		for (LeaveType leaveType : leaveTypes) {
			leaveTypesMap.put(leaveType.getLeaveType(), leaveType.getLeaveTypeid());
		}
		return null;
	}

	

	// GETTERS AND SETTERS
	public Map<String, Long> getLeaveTypesMap() {
		return leaveTypesMap;
	}

	public void setLeaveTypesMap(Map<String, Long> leaveTypesMap) {
		this.leaveTypesMap = leaveTypesMap;
	}
	public LeaveTypeDAO getLeaveTypeDAO() {
		return leaveTypeDAO;
	}

	public void setLeaveTypeDAO(LeaveTypeDAO leaveTypeDAO) {
		this.leaveTypeDAO = leaveTypeDAO;
	}

	public List<LeaveType> getLeaveTypes() {
		return leaveTypes;
	}

	public void setLeaveTypes(List<LeaveType> leaveTypes) {
		this.leaveTypes = leaveTypes;
	}

	public LeaveType getLeaveType() {
		return leaveType;
	}

	public void setLeaveType(LeaveType leaveType) {
		this.leaveType = leaveType;
	}

	public Long getSelectedLeaveTypeId() {
		return selectedLeaveTypeId;
	}

	public void setSelectedLeaveTypeId(Long selectedLeaveTypeId) {
		this.selectedLeaveTypeId = selectedLeaveTypeId;
	}

	public LeaveType getSelectedLeaveType() {
		return selectedLeaveType;
	}

	public void setSelectedLeaveType(LeaveType selectedLeaveType) {
		this.selectedLeaveType = selectedLeaveType;
	}

	public boolean isDetailsForLeaveType() {
		return detailsForLeaveType;
	}

	public void setDetailsForLeaveType(boolean detailsForLeaveType) {
		this.detailsForLeaveType = detailsForLeaveType;
	}

	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

}
